package com.hera.base.web.vaadin;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.Constantes;
import com.hera.base.util.FiltroDTO;
import com.hera.base.web.BaseUI;
import com.hera.process.config.rest.delegate.FormDelegate;
import com.hera.process.config.rest.delegate.WorkflowDelegate;
import com.hera.process.form.domain.Dynaform;
import com.hera.process.form.domain.DynaformField;
import com.hera.process.form.domain.DynaformStage;
import com.hera.process.form.domain.FieldOption;
import com.hera.process.workflow.domain.Process;
import com.hera.process.workflow.domain.Stage;
import com.hera.process.workflow.domain.Swimlane;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class FormUI extends BaseUI {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private Button btnRechazar;
	@AutoGenerated
	private Button btnAceptar;
	@AutoGenerated
	private GridLayout controlLayout;
	private Process process;
	
	@Autowired
	private WorkflowDelegate workflowDelegate;
	
	@Autowired
	public FormDelegate formDelegate;
	
	public int nRows;
	
	
	private List<DynaformField> dynaformfields;
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 * @param object 
	 */
	
	public FormUI(Process process) {
		this.process=process;
		buildMainLayout();
		setCompositionRoot(mainLayout);
	
	}
	public List<DynaformField> lista()  {
		Long idProceso =process.getIdProcess();
		Stage stage = new Stage();
		Swimlane swimlane = new Swimlane();
		Process process = new Process();
		process.setIdProcess(idProceso);
		process.setIdType(Long.valueOf(Constantes.NODETYPE.ID_BEGIN));
		swimlane.setProcess(process);
		stage.setSwimlane(swimlane);
		
		List<Stage> stages=workflowDelegate.findStageByProcessIdandTypeId(stage);
		stage.setIdStage(stages.get(0).getIdStage());
		
		List<DynaformStage> lstDynaformStages=formDelegate.findAllDynaformStagesByStage(stage);
		
		FiltroDTO filtroDTO= new FiltroDTO();
		filtroDTO.addParametro("idDynaform", lstDynaformStages.get(0).getDynaform().getIdDynaform());
		List<Dynaform> lstDynaForms=formDelegate.findAllDynaforms(filtroDTO);
		
		Dynaform dynaForm= new Dynaform();
		dynaForm.setIdDynaform(lstDynaForms.get(0).getIdDynaform());
		
		
		FiltroDTO filtroDTO1= new FiltroDTO();
		filtroDTO1.addParametro("idDynaform", dynaForm.getIdDynaform());

		
		 dynaformfields =formDelegate.findAllDynaFormFieldByIdDynaForm(filtroDTO1);
		
		 nRows=dynaformfields.size();
		 
		 
		for (DynaformField dynaformfield : dynaformfields) {
			
			Label lblLabels= new Label();
			lblLabels.setValue(dynaformfield.getField().getName());
			lblLabels.setVisible(true);
			controlLayout.addComponent(lblLabels);
			
			if(Constantes.FORMTYPECOMPONENT.CHECKBOX==dynaformfield.getIdType()){
				CheckBox chBox= new CheckBox("CheckBox");
				chBox.setVisible(true);
				controlLayout.addComponent(chBox);
				
			}
			else if(Constantes.FORMTYPECOMPONENT.TEXTFIELD==dynaformfield.getIdType()){
				TextField txt= new TextField();
				txt.setVisible(true);
				controlLayout.addComponent(txt);
				
			}
			else if(Constantes.FORMTYPECOMPONENT.COMBOBOX==dynaformfield.getIdType()){
				
				FiltroDTO filtroDTO2= new FiltroDTO();
				filtroDTO2.addParametro("formField.idField", dynaformfield.getField().getIdField());
				dynaformfield.getField().setFieldOptionList(formDelegate.findAllFieldOptions(filtroDTO2));	
				
				ArrayList<FieldOption> lstFieldOptions=(ArrayList<FieldOption>)dynaformfield.getField().getFieldOptionList();
				
				ComboBox cbo= new ComboBox();
				cbo.setVisible(true);
				for (FieldOption fieldOption : lstFieldOptions) {
					cbo.addItem(fieldOption.getValue());
				}
				
				controlLayout.addComponent(cbo);
				
			}
			else if(Constantes.FORMTYPECOMPONENT.TEXTAREA==dynaformfield.getIdType()){
				TextArea txtArea= new TextArea();
				txtArea.setVisible(true);
				controlLayout.addComponent(txtArea);
			}
			else if(Constantes.FORMTYPECOMPONENT.INPUTDATE==dynaformfield.getIdType()){
				 DateField calendar= new DateField();
				 calendar.setVisible(true);
				 controlLayout.addComponent(calendar);
			}
			else if(Constantes.FORMTYPECOMPONENT.OPTIONGROUP==dynaformfield.getIdType()){
				FiltroDTO filtroDTO2= new FiltroDTO();
				filtroDTO2.addParametro("formField.idField", dynaformfield.getField().getIdField());
				dynaformfield.getField().setFieldOptionList(formDelegate.findAllFieldOptions(filtroDTO2));
				ArrayList<FieldOption> lstFieldOptions=(ArrayList<FieldOption>)dynaformfield.getField().getFieldOptionList();

				OptionGroup rb= new OptionGroup();
				
				for (FieldOption fieldOption : lstFieldOptions) {
					rb.addItem(fieldOption.getValue());
				}
				controlLayout.addComponent(rb);
				
			}
			else if(Constantes.FORMTYPECOMPONENT.LABEL==dynaformfield.getIdType()){
				Label lblLabel= new Label();
				lblLabel.setValue(dynaformfield.getField().getLabel());
				lblLabel.setVisible(true);
				controlLayout.addComponent(lblLabel);
				
			}		
		}
		return dynaformfields;
			
		
	}

	@Override
	protected void postConstruct() {
		
	}
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnRechazar)) {
			cerrarForm();
		}
	}
	
	public void cerrarForm(){

	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// controlLayout
		controlLayout = new GridLayout();
		controlLayout.setImmediate(false);
		controlLayout.setWidth("100.0%");
		controlLayout.setHeight("95.0%");
		controlLayout.setMargin(false);
		controlLayout.setColumns(2);
		mainLayout.addComponent(controlLayout);
		mainLayout.setComponentAlignment(controlLayout, new Alignment(48));
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		mainLayout.addComponent(horizontalLayout_1);
		mainLayout.setComponentAlignment(horizontalLayout_1, new Alignment(24));
		
		return mainLayout;
	}
	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("100.0%");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		
		// btnAceptar
		btnAceptar = new Button();
		btnAceptar.setCaption("Aceptar");
		btnAceptar.setImmediate(true);
		btnAceptar.setWidth("-1px");
		btnAceptar.setHeight("-1px");
		horizontalLayout_1.addComponent(btnAceptar);
		horizontalLayout_1.setComponentAlignment(btnAceptar, new Alignment(20));
		
		// btnRechazar
		btnRechazar = new Button();
		btnRechazar.setCaption("Rechazar");
		btnRechazar.setImmediate(true);
		btnRechazar.setWidth("-1px");
		btnRechazar.setHeight("-1px");
		horizontalLayout_1.addComponent(btnRechazar);
		horizontalLayout_1
				.setComponentAlignment(btnRechazar, new Alignment(20));
		
		return horizontalLayout_1;
	}

}
